package com.zh.book_borrow.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.zh.book_borrow.Exception.MyException;
import com.zh.book_borrow.entity.User;
import com.zh.book_borrow.mapper.UserMapper;
import com.zh.book_borrow.service.UserService;
import com.zh.book_borrow.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserserviceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO login(String username, String password) throws MyException {
        User user = userMapper.findByUsername(username);
        if(Objects.isNull(user)){
            throw new MyException("用户不存在");
        }
        if(!user.getPassword().equals(password)){
            throw new MyException("密码错误");
        }

        // 登录成功
        StpUtil.login(user.getId());
        String tokenValue = StpUtil.getTokenInfo().getTokenValue();
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO,"password");
        userVO.setToken(tokenValue);
        return userVO;
    }

    @Override
    public User getInfo(Long userId) {
        User user= userMapper.findById(userId);
        return null;
    }
}
